package com.example.leetcode.leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author zht
 * @date 2021/1/14 11:13
 */
public class Solution104 {
    // DFS
    public int maxDepth(TreeNode root) {
        if(null == root){
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left,right);
    }

    // BFS
    public int maxDepth2(TreeNode root) {
        if(null == root){
            return 0;
        }
        Queue<TreeNode>queue = new LinkedList<>();
        queue.add(root);
        int level = 0;
        while (!queue.isEmpty()){
            int n = queue.size();
            for (int i=0; i<n;i++) {
                TreeNode node = queue.remove();
                if (null != node.left) {
                    queue.add(node.left);
                }
                if (null != node.right) {
                    queue.add(node.right);
                }
            }
            level++;
        }

        return level;
    }
}
